package org.wisigoth.chat.swing.accountframe;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import org.wisigoth.chat.client.UserPreferences;
import org.wisigoth.chat.client.UserPreferencesEvent;
import org.wisigoth.chat.client.UserPreferencesListener;
import org.wisigoth.chat.client.Wisigoth;




/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginWindow.java
 *
 * Created on 20 janv. 2009, 13:46:58
 */



/**
 *
 * @author grafiton
 */
public class LoginWindow extends javax.swing.JFrame {

    private Wisigoth wisigoth;
    private PreferencesListener listener;
    
	/** Creates new form LoginWindow */
    public LoginWindow() {
        this.initComponents();
        this.listener = this.new PreferencesListener();
    }
    
    public void setModel(Wisigoth wisigoth){
    	if(wisigoth == null){
    		throw new IllegalArgumentException("Null model");
    	}
    	wisigoth.getUserPreferences().removeUserPreferencesListener(this.listener);
    	this.wisigoth = wisigoth;
    	this.initStateFromModel();
    	this.wisigoth.getUserPreferences().addUserPreferencesListener(this.listener);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        this.userLabel = new javax.swing.JLabel();
        this.userTextField = new javax.swing.JTextField();
        this.pwdLabel = new javax.swing.JLabel();
        this.pwdPasswordField = new javax.swing.JPasswordField();
        this.savePwdCheckBox = new javax.swing.JCheckBox();
        this.connectButton = new javax.swing.JButton();
        this.createaccountPane1 = new javax.swing.JLayeredPane();
        GridBagLayout createaccountPane1Layout = new GridBagLayout();
        this.createAccountButton = new javax.swing.JButton();

        this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        this.setTitle("Wisigoth Login");
        this.setMinimumSize(new java.awt.Dimension(250, 400));
        this.setResizable(false);
        this.getContentPane().setLayout(new java.awt.GridBagLayout());

        this.userLabel.setText("Name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(100, 20, 0, 1);
        this.getContentPane().add(this.userLabel, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 20;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 150;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(100, 0, 0, 20);
        this.getContentPane().add(this.userTextField, gridBagConstraints);

        this.pwdLabel.setText("Password");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.ipady = 5;
        gridBagConstraints.insets = new java.awt.Insets(0, 20, 0, 0);
        this.getContentPane().add(this.pwdLabel, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 20;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 150;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 20);
        this.getContentPane().add(this.pwdPasswordField, gridBagConstraints);

        this.savePwdCheckBox.setText("Save password");
        this.savePwdCheckBox.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 20;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 20);
        this.getContentPane().add(this.savePwdCheckBox, gridBagConstraints);

        this.connectButton.setText("Connect");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 21;
        gridBagConstraints.ipadx = 30;
        gridBagConstraints.insets = new java.awt.Insets(20, 0, 40, 0);
        this.getContentPane().add(this.connectButton, gridBagConstraints);
        
        GridBagConstraints accountButtonConstraints = new GridBagConstraints();
        accountButtonConstraints.gridx = 0;
        accountButtonConstraints.gridy = 0;
        accountButtonConstraints.anchor = GridBagConstraints.CENTER;
        accountButtonConstraints.gridwidth = 1;
        accountButtonConstraints.gridheight = 1;
        this.createaccountPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("New to Wisigoth ?"));
        this.createaccountPane1.setFocusable(false);
        this.createaccountPane1.setMaximumSize(new java.awt.Dimension(100, 100));
        this.createaccountPane1.setLayout(createaccountPane1Layout);
        this.createaccountPane1.setPreferredSize(new java.awt.Dimension(100, 50));
        
        this.createAccountButton.setText("Create account");
        this.createAccountButton.setBounds(60, 30, 160, 23);
        this.createaccountPane1.add(this.createAccountButton, accountButtonConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 21;
        gridBagConstraints.gridheight = 20;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 20;
        gridBagConstraints.insets = new java.awt.Insets(100, 0, 0, 0);
        this.getContentPane().add(this.createaccountPane1, gridBagConstraints);
        this.pack();
        this.setLocationByPlatform(true);

    }// </editor-fold>//GEN-END:initComponents
    
    private void initStateFromModel(){
    	String login = this.wisigoth.getUserPreferences().getLoginPreference();
    	char[] pwd = this.wisigoth.getUserPreferences().getPasswordPreference();
    	if (login != null){
    		this.userTextField.setText(login);
    		if (pwd != null) {
    			this.pwdPasswordField.setText(new String(pwd));
        		this.savePwdCheckBox.setSelected(true);	
    		} else {
    			this.pwdPasswordField.setText("");
        		this.savePwdCheckBox.setSelected(false);	
    		}
    		
    	}
    }
    
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LoginWindow().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton connectButton;
    private javax.swing.JButton createAccountButton;
    private javax.swing.JLayeredPane createaccountPane1;
    private javax.swing.JLabel pwdLabel;
    public javax.swing.JPasswordField pwdPasswordField;
    private javax.swing.JCheckBox savePwdCheckBox;
    private javax.swing.JLabel userLabel;
    public javax.swing.JTextField userTextField;
    // End of variables declaration//GEN-END:variables
	public javax.swing.JButton getConnectButton() {
		return this.connectButton;
	}

	public javax.swing.JButton getCreateAccountButton() {
		return this.createAccountButton;
	}

	public javax.swing.JPasswordField getPwdPasswordField() {
		return this.pwdPasswordField;
	}

	public javax.swing.JCheckBox getSavePwdCheckBox() {
		return this.savePwdCheckBox;
	}

	public javax.swing.JTextField getUserTextField() {
		return this.userTextField;
	}
	
	private class PreferencesListener implements UserPreferencesListener {

		@Override
		public void userPreferencesUpdated(UserPreferencesEvent event) {
			if (UserPreferences.LOGIN_KEY.equals(event.getUpdatedPreference())){
				LoginWindow.this.userTextField.setText(event.getSource().getLoginPreference());
			}else if(UserPreferences.PWD_KEY.equals(event.getUpdatedPreference())){
				char[] pwdPref = event.getSource().getPasswordPreference();
				if (pwdPref != null){
					LoginWindow.this.pwdPasswordField.setText(new String(pwdPref));
					LoginWindow.this.savePwdCheckBox.setSelected(true);
				}else {
					LoginWindow.this.pwdPasswordField.setText("");
					LoginWindow.this.savePwdCheckBox.setSelected(false);
				}
			}
		}
		
	}
}
